<?php
require('config.php');
		function _getFloatLength($num) {
			$count = 0;
			$temp = explode ( '.', $num );
			if (sizeof ( $temp ) > 1) {
			$decimal = end ( $temp );
			$count = strlen ( $decimal );
			}
			return $count;
		}
        $goods_id=$_REQUEST['goods_id'];
        $user_id=$_REQUEST['user_id'];
        $time=time();
		$sql="select * from `".$oto."_integral` where `goodsId`='".$goods_id."' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$jf_goods[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_users` where `userId`='".$user_id."' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$users[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($jf_goods))
		{
			if(!isset($users))
			{
				$data[0]['pan']='-1';
				$data[0]['msg']='用户不存在';
				echo json_encode($data);
				die();
			}
			if($users[0]['userScore']<$jf_goods[0]['luckyPrice'])
			{
				$data[0]['pan']='-1';
				$data[0]['msg']='积分不足';
				echo json_encode($data);
				die();
			}
			if($jf_goods[0]['endTime']<$time)
			{
				$data[0]['pan']='-1';
				$data[0]['msg']='活动已结束';
				echo json_encode($data);
				die();
			}
			$pro=$jf_goods[0]['chance'];//概率
			$dec_num=_getFloatLength($pro);//小数点后的位数
			$max=100*pow(10,$dec_num);//最大范围
			$pro_end=$max*($pro/100);//最大中奖随机数
			$sjs=rand(1,$max);//随机的数字
			$data[0]['sjs']=$sjs;
			if($sjs>=1 && $sjs<=$pro_end && $jf_goods[0]['getLucky']<$jf_goods[0]['winningNum'])
			{
				$time2=microtime(true)*10000;
				$goodsId=$jf_goods[0]['goodsId'];
				$luckNo=chr(fmod($time2,26)+65).$goodsId.$time2;
				//增加积分抽奖表中奖次数
				$sql="update ".$oto."_integral set getLucky=getLucky+1 where `goodsId`='".$goods_id."'";
				$result=$db->query($sql);
				//减少用户积分
				$huserScore=$users[0]['userScore']-$jf_goods[0]['luckyPrice'];
				$sql="update ".$oto."_users set userScore='{$huserScore}' where userId='{$user_id}'";
				$result=$db->query($sql);
				//生成订单
				$orderNo=$time2;
				$orderStatus=0;
				$totalMoney=$jf_goods[0]['luckyPrice'];
				$orderScore=$jf_goods[0]['luckyPrice'];
				$requireTime=0;//期望送达时间
				$createTime=date('Y-m-d H:i:s');//下单时间
				$orderunique=floor($time2);//是否同一批下单
				$needPay=$jf_goods[0]['luckyPrice'];//应付积分
				$sql="INSERT INTO ".$oto."_orders (`orderNo`,`orderStatus`,`totalMoney`,`deliverType`,`userId`,`orderScore`,`orderRemarks`,`createTime`,`orderunique`,`needPay`,`orderType`,`payType`,`paytime`,`winningCode`) VALUES ('$orderNo','$orderStatus','$totalMoney','0','$user_id','$orderScore','','$createTime','$orderunique','$needPay','5','4','$time','$luckNo')";
		        $result2=$db->query($sql);//插入order表
		        //file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
		        $sql="select * from `".$oto."_orders` where `orderNo`='".$orderNo."' and `userId`='{$user_id}' and `orderunique`='{$orderunique}'";
				$result=$db->query($sql);//查询刚插进去的orderId
				while($row=$result->fetch_assoc()){
					$order[0]=$row;//将取得的所有数据赋值给person_info数组
				}
				$orderId=$order[0]['orderId'];
				$goods_id=$goodsId;
				$goodsPrice=$totalMoney;
				$goodsName=$jf_goods[0]['goodsName'];
				$goodsThums=$jf_goods[0]['goodsImg'];
		        $sql="INSERT INTO ".$oto."_order_goods (`orderId`,`goodsId`,`goodsNums`,`goodsPrice`,`goodsName`,`goodsThums`,`goodsGroupId`) VALUES ('$orderId','$goods_id','1','$goodsPrice','$goodsName','$goodsThums','0')";
		        $result=$db->query($sql);//插入order对应的order_goods表
		        //增加积分消费记录
				$sql="INSERT INTO ".$oto."_score_record (`userid`,`orderNo`,`score`,`totalscore`,`time`,`IncDec`,`type`) VALUES ('$user_id','$orderNo','$totalMoney','$huserScore','$time','0','8')";
	        	$result=$db->query($sql);
		        //返回数据
				$data[0]['pan']='1';
				$data[0]['msg']='恭喜你中奖';
				$data[0]['luckNo']=$luckNo;
				echo json_encode($data);
				die();
			}
			else
			{
				$time2=microtime(true)*10000;
				$goodsId=$jf_goods[0]['goodsId'];
				//$luckNo=chr($time2 % 26+65).$goodsId.$time2;
				//减少用户积分
				$huserScore=$users[0]['userScore']-$jf_goods[0]['luckyPrice'];
				$sql="update ".$oto."_users set userScore='{$huserScore}' where userId='{$user_id}'";
				$result=$db->query($sql);
				//生成订单
				$orderNo=$time2;
				$orderStatus=0;
				$totalMoney=$jf_goods[0]['luckyPrice'];
				$orderScore=$jf_goods[0]['luckyPrice'];
				$requireTime=0;//期望送达时间
				$createTime=date('Y-m-d H:i:s');//下单时间
				$orderunique=floor($time2);//是否同一批下单
				$needPay=$jf_goods[0]['luckyPrice'];//应付积分
				$sql="INSERT INTO ".$oto."_orders (`orderNo`,`orderStatus`,`totalMoney`,`deliverType`,`userId`,`orderScore`,`orderRemarks`,`createTime`,`orderunique`,`needPay`,`orderType`,`payType`,`paytime`) VALUES ('$orderNo','$orderStatus','$totalMoney','0','$user_id','$orderScore','','$createTime','$orderunique','$needPay','5','4','$time')";
		        $result2=$db->query($sql);//插入order表
		        //file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
		        $sql="select * from `".$oto."_orders` where `orderNo`='".$orderNo."' and `userId`='{$user_id}' and `orderunique`='{$orderunique}'";
				$result=$db->query($sql);//查询刚插进去的orderId
				while($row=$result->fetch_assoc()){
					$order[0]=$row;//将取得的所有数据赋值给person_info数组
				}
				$orderId=$order[0]['orderId'];
				$goods_id=$goodsId;
				$goodsPrice=$totalMoney;
				$goodsName=$jf_goods[0]['goodsName'];
				$goodsThums=$jf_goods[0]['goodsImg'];
		        $sql="INSERT INTO ".$oto."_order_goods (`orderId`,`goodsId`,`goodsNums`,`goodsPrice`,`goodsName`,`goodsThums`,`goodsGroupId`) VALUES ('$orderId','$goods_id','1','$goodsPrice','$goodsName','$goodsThums','0')";
		        $result=$db->query($sql);//插入order对应的order_goods表
		        //增加积分消费记录
				$sql="INSERT INTO ".$oto."_score_record (`userid`,`orderNo`,`score`,`totalscore`,`time`,`IncDec`,`type`) VALUES ('$user_id','$orderNo','$totalMoney','$huserScore','$time','0','8')";
	        	$result=$db->query($sql);
				$data[0]['pan']='0';
				$data[0]['msg']='很遗憾，您没中奖';
				echo json_encode($data);
				die();
			}
		}
		else
		{
			$data[0]['pan']='-1';
			$data[0]['msg']='商品已下架';
		}
		echo json_encode($data);
?>